//
//  SHJSChangeViewController.m
//  SDemo
//
//  Created by gg on 2018/7/20.
//  Copyright © 2018年 gg. All rights reserved.
//

#import "SHJSChangeViewController.h"

@interface SHJSChangeViewController ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webview;


@end

@implementation SHJSChangeViewController


-(UIWebView *)webview {
    if (_webview == nil) {
        _webview = [[UIWebView alloc] init];
        _webview.delegate = self;
        _webview.backgroundColor  = [UIColor yellowColor];
    }
    
    return  _webview;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"原生和js交互";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webview];
    
    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(64);
    }];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"index1" ofType:@"html"];
   // NSString *path = [[NSBundle mainBundle] pathForResource:@"index2" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:request];
    
    
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //js 调用原生方法
    //第一种方法
    NSURL *url = [request URL];
    
    if ([[url scheme] isEqualToString:@"firstclick"]) { //判断是否点击了网页上的button
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSArray *temArr = [url.query componentsSeparatedByString:@"&"];
        for (NSString * paramStr in temArr) {
            NSArray *newTemArr = [paramStr componentsSeparatedByString:@"="];
            if (newTemArr.count > 1) {
                [dic setObject:newTemArr[1] forKey:newTemArr[0]];
            }
            
        }
        
        UIAlertController *alterC = [UIAlertController alertControllerWithTitle:@"方式一" message:@"这是原生的弹出窗" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *cancal = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alterC addAction:cancal];
        [self presentViewController:alterC animated:YES completion:nil];
        NSLog(@"temDic :%@",dic);
        return NO;
    }
    
   
    
    return YES;
}


-(void)webViewDidFinishLoad:(UIWebView *)webView {
     //第二种：使用JavaScriptCore
    //获取上下文
//    JSContext *context = [self.webview valueForKey:@"documentView.webView.mainFrame.javaScriptContext"];
//    context[@"share"] = ^() {
//        NSLog(@"begin ----");
//        NSArray *param = [JSContext currentArguments];
//        for (JSValue *jsval in param) {
//            NSLog(@"jsval : %@",jsval.toString);
//        }
//
//        __weak typeof(self) weakSelf = self;
//        dispatch_async(dispatch_get_main_queue(), ^{
//            UIAlertController *alterC = [UIAlertController alertControllerWithTitle:@"方式二" message:@"这是原生的弹出窗" preferredStyle:(UIAlertControllerStyleAlert)];
//            UIAlertAction *cancal = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//
//            }];
//            [alterC addAction:cancal];
//            [weakSelf presentViewController:alterC animated:YES completion:nil];
//        });
//        NSLog(@"finish --- ");
//    };
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
